package com.shop.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.shop.dao.ProductCategoryDao;
import com.shop.entity.Product;
import com.shop.entity.ProductCategory;
@Repository
@Transactional
public class ProductCategoryDaoImpl extends HibernateTemplate implements ProductCategoryDao{

	@Autowired
	private SessionFactory sessionFactory ;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public List<ProductCategory> findAllProductCategory() {
		List<ProductCategory> pclist = (List<ProductCategory>) super.find("from ProductCategory");
		return pclist;
	}

	@Override
	public Serializable addProductCategory(ProductCategory productCategory) {
		return super.save(productCategory);
	
	}

	@Override
	public Serializable deleteProductCategory(ProductCategory productCategory) {
		super.delete(productCategory);
		return null;
	}

	@Override
	public Serializable updateProductCategory(ProductCategory productCategory) {
		super.update(productCategory);
		return null;
	}

	@Override
	public ProductCategory selectProductCategory(ProductCategory productCategory) {
		//根据商品类别的id 查询商品类别信息
			String hql = "from ProductCategory where epc_id=?";
			Session session = sessionFactory.openSession();
			Query q = session.createQuery(hql);
			q.setInteger(0, productCategory.getEpcId());
			ProductCategory pc = (ProductCategory)q.uniqueResult();
			return pc;
	}

}
